package cn.biplam.front.component.view.systemManage.sysprivi.privilege.navi;

import cn.biplam.common.ajxcode.SystemCode.PriviNaviCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.PriviDeployOptCode;
import cn.biplam.common.struc.SysStruc.PriviDeployStruc;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.struc.SysStruc.RolePriviStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.data.tree.treebase.TreeCallBackNew;
import cn.biplam.front.component.data.tree.treebase.TreeDcIndex;
import cn.biplam.front.component.view.tree.TreeBase;
import cn.biplam.front.component.view.tree.TreeView;

/**
 *  权限导航树
 * @author XiaoRuxing Dec 9, 20097:49:55 PM
 * @version 1.0
 */
public class PriviNavi extends TreeView{

	public final static String cId=PriviNaviCode.cId;
	/** 树展开动作ID */
	public static final String Expand = PriviNaviCode.Expand;
	
	/** 过滤条件操作码 */
	private String filterOptId;
	/** 过滤条件 */
	private String roleId;
	
	public PriviNavi() {
		super(Expand);
	}

	/**
	 * 传入树节点链接到的目标容器ID初始化
	 * @param targetId
	 */
	public void setTarget(String targetId) {
		this.targetId=targetId;
	}
	
	/**
	 *  设置过滤条件操作码
	 * @param filterOptId 过滤操作码
	 * @param filter      节点过滤条件
	 */
	public void setFilterOperator(String filterOptId,String roleId){
		this.filterOptId = filterOptId;
		this.roleId = roleId;
	}
	
	/**
	 * 设置树的数据信息
	 * @param treeBase			树的基础VC
	 * @param mUser				页面请求的用户类(由调用树的vc传入)
	 * @throws ErrorMessage
	 */
	@Override
	protected void setTree(TreeBase treeBase, UserInfo mUser)
			throws ErrorMessage {
		//去所有已发布的权限大类
		OperatorResult rs = Operator.get(PriviDeployOptCode.Query_All, 
				mUser.getUserID(), new DataFilter());
		
		if(!TOOLS.exist(rs))return; //无权限大类
		//每个权限类型创建一颗树
		do{
			this.putTree(treeBase, 
						 mUser, 
						 Integer.parseInt(rs.getValue(PriviDeployStruc.SEQ)),
						 rs.getValue(PriviDeployStruc.ROOT_ID),//根ID
						 rs.getValue(PriviDeployStruc.NAME)	//大类名称
						 );
		}while(rs.next());
	}
	
	/**
	 * 使用传入参数,创建一颗权限类型的子树,并放入TreeBase中
	 */
	private void putTree(TreeBase treeBase, 
						 UserInfo mUser,
						 int iTreeIndex,
						 String treeVirtualRootId,
						 String treeVirtualName
				) throws ErrorMessage{
		/**树的callback设置*/
		TreeCallBackNew callback = new TreeCallBackNew();
		//设置用户左键事件链接到显示角色信息
		//创建树DC
		TreeDcIndex curDc = new TreeDcIndex(mUser,//用户信息
				PriviStruc.Table_Name,   //用户索引表名
				null,
				treeVirtualRootId,
				iTreeIndex==1?"1":"1"   //展开第一棵权限子树
					);
		//设置节点过滤器
		String roleFilterName = ".privi_filter_role";
		if(params!=null){
			this.roleId = params.getByKey(roleFilterName);
		}
		treeBase.setCustomerParam(roleFilterName,this.roleId);
		//设置过滤条件
		DataFilter filter = new DataFilter();
		filter.put(RolePriviStruc.ROLE_ID, 
				this.roleId //当前角色ID
		);
		curDc.setNodeFilter(filter);
		//设置过滤操作
		if(this.filterOptId!=null){
			curDc.setFilterOperatorId(this.filterOptId);
		}
		//设置树DC
		treeBase.putTreeDc(iTreeIndex, 
				curDc,
				callback);
		
		treeBase.setShowVirRoot(true,true);//设置显示虚根,并且展开
		treeBase.setRightMenu(false);//不显示右键菜单
//		treeBase.setShowCheckBox(true);//以选择框提供选择
		
	}
}
